package com.quectel.core.module.dept.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.quectel.constant.global.SystemConstants;
import com.quectel.core.constants.RedisCacheConstants;
import com.quectel.core.module.dept.dao.DeptPersonMsgDao;
import com.quectel.core.module.dept.dto.DeptPersonMsgDto;
import com.quectel.core.module.dept.entity.DeptPersonMsgEntity;
import com.quectel.core.module.dept.service.DeptPersonMsgService;
import com.quectel.util.common.CopyUtils;
import com.quectel.util.kit.CacheKit;
import com.quectel.util.kit.Snowflake;
import com.quectel.util.mybatis.MyBatisPlusUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author caolu
 * @email louis.cao@quectel.com
 * @date 2022/05/07 14:04
 */
@DubboService
public class DeptPersonMsgServiceImpl extends ServiceImpl<DeptPersonMsgDao, DeptPersonMsgEntity> implements DeptPersonMsgService {

    private static final Logger LOGGER = LoggerFactory.getLogger(DeptPersonMsgServiceImpl.class);

    /**
     * 入参解释:实体id
     */
    private static final Function<Long, String> DEPT_PERSON_MSG_CACHE_BY_ID_FUNC = id -> String.format(RedisCacheConstants.PROJECT_NAME + "DEPT_PERSON_MSG_CACHE_BY_ID:%d", id);

    @Autowired
    private DeptPersonMsgDao deptPersonMsgDao;


    @Override
    public DeptPersonMsgDto selectCacheById(Long id) {
        return CacheKit.cacheToRedis(
                () -> selectById(id),
                DEPT_PERSON_MSG_CACHE_BY_ID_FUNC.apply(id),

                SystemConstants.NOT_NULL_CACHE_EXPIRE_SECONDS
        );
    }

    @Override
    public DeptPersonMsgDto selectById(Long id) {
        DeptPersonMsgEntity entity = deptPersonMsgDao.selectById(id);
        return paddingField(CopyUtils.copyObj(entity, DeptPersonMsgDto.class));
    }

    @Override
    public List<DeptPersonMsgDto> queryList(Map<String, Object> params) {
        List<DeptPersonMsgEntity> list = deptPersonMsgDao.queryList(params);
        List<DeptPersonMsgDto> result = CopyUtils.copyList(list, DeptPersonMsgDto.class);
        result.forEach(this::paddingField);
        return result;
    }

    private DeptPersonMsgDto paddingField(DeptPersonMsgDto dto) {
        if (dto != null) {

        }
        return dto;
    }

    @Override
    public int queryTotal(Map<String, Object> params) {
        return deptPersonMsgDao.queryTotal(params);
    }

    @Override
    public Long save(DeptPersonMsgDto dto) {
        DeptPersonMsgEntity entity = CopyUtils.copyObj(dto, DeptPersonMsgEntity.class);
        entity.setId(Snowflake.nextId());
        deptPersonMsgDao.insert(entity);
        return entity.getId();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveBatch(List<DeptPersonMsgDto> deptPersonMsgDtoList) {
        if (CollectionUtils.isEmpty(deptPersonMsgDtoList)) {
            return;
        }

        List<DeptPersonMsgEntity> msgEntities = deptPersonMsgDtoList.stream()
                .map(item -> {
                    DeptPersonMsgEntity deptPersonMsgEntity = CopyUtils.copyObj(item, DeptPersonMsgEntity.class);
                    deptPersonMsgEntity.setId(Snowflake.nextId());
                    return deptPersonMsgEntity;
                }).collect(Collectors.toList());
        super.saveBatch(msgEntities);
    }

    @Override
    public void updateById(DeptPersonMsgDto dto) {
        DeptPersonMsgEntity entity = CopyUtils.copyObj(dto, DeptPersonMsgEntity.class);
        deptPersonMsgDao.updateById(entity);
        CacheKit.invalidRedisCache(DEPT_PERSON_MSG_CACHE_BY_ID_FUNC.apply(dto.getId()));
    }

    @Override
    public void updateAllColumnById(DeptPersonMsgDto dto) {
        DeptPersonMsgEntity entity = CopyUtils.copyObj(dto, DeptPersonMsgEntity.class);


        MyBatisPlusUtils.updateAllColumnById(entity, deptPersonMsgDao);

        CacheKit.invalidRedisCache(DEPT_PERSON_MSG_CACHE_BY_ID_FUNC.apply(dto.getId()));
    }

    @Override
    public void deleteById(Long id) {
        deptPersonMsgDao.deleteById(id);

        CacheKit.invalidRedisCache(DEPT_PERSON_MSG_CACHE_BY_ID_FUNC.apply(id));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBatch(Long[] ids) {
        for (Long id : ids) {
            deleteById(id);
        }
    }
}
